package com.globallogic.training.javase.basics;

import java.util.Scanner;

/**
* The console application finding two neighbouring numbers in an array
* with the smallest distance to each other. Application outputs the index of the first number.
*
* @version 1.2
* @author Artem.Zeltinsh
*/
public class NeighbouringNumbers
{
	private static final String WELCOME_MESSAGE =
				"# The console application finding two neighbouring numbers in an array\n" +
				"#  with the smallest distance to each other.\n";

	private static final String PROMPT_MESSAGE = "Please enter sequence of 2 or more integer numbers separated by space: \n";

	private static final String OUTPUT_MESSAGE_FORMAT = "Index: %d";

	public static void main(String[] args) {
		int[] inputNumbers = readInputNumbers();

		int resultIndex = 0;
		int minDistance = Math.abs(inputNumbers[1] - inputNumbers[0]);
		for (int i = 0; i < inputNumbers.length - 1; i++) {
			int currentDistance = Math.abs(inputNumbers[i+1] - inputNumbers[i]);
			if (currentDistance < minDistance) {
				minDistance = currentDistance;
				resultIndex = i;
			}
		}

		System.out.printf(OUTPUT_MESSAGE_FORMAT, resultIndex);
    }

	private static int[] readInputNumbers() {
		System.out.println(WELCOME_MESSAGE);
		
		int[] result;
		do {
			System.out.print(PROMPT_MESSAGE);
			
			Scanner inputLine = new Scanner(System.in);
			String[] inputTokens = inputLine.nextLine().split("\\s"); 
			result = new int[inputTokens.length];
			
			for (int i = 0; i < inputTokens.length; i++)
			{
				result[i] = Integer.parseInt(inputTokens[i]);
			}
		} while (result.length < 2);
		
		return result;
	}
}
